package com.xgkj.diyiketang.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.xgkj.diyiketang.data.PhotoInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.ZanzhaopianTask;
import com.xgkj.diyiketang.task.ZhaopianDetailTask;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.util.zoom.PhotoView;
import com.xgkj.diyiketang.util.zoom.ViewPagerFixed;
import com.xgkj.diyiketang.R;

import java.util.ArrayList;

public class XiangceBigActivity extends BaseActivity implements View.OnClickListener{
    private final String TAG = getClass().getSimpleName();
    private Context mContext;
    private ImageView commentView;
    private ImageView zanView;
    private TextView toComView;
//    private PhotoView mImageView;
    private PhotoInfo mPhotoInfo;
    private String type = "0";
    private TextView jubaoText;
    private int zanTaskId = -1;
    private int detailTaskId = -1;
    private ArrayList<PhotoInfo> photoList = new ArrayList<PhotoInfo>();
    String url;
    private ViewPagerFixed viewPagerFixed;

    private GalleryAdapter mAdapter;
    private ArrayList<View> mList;

    private int currentPosition;
    private boolean isFirst = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xiangce_big);
        mContext = this;
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
//        mPhotoInfo = (PhotoInfo) intent.getSerializableExtra("photoInfo");
        photoList = (ArrayList<PhotoInfo>) intent.getSerializableExtra("photoInfo");
        currentPosition = intent.getIntExtra("currentPosition",0);
        if (photoList == null || photoList.size()==0){
            finish();
            return;
        }
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
//        mImageView = (PhotoView) findViewById(R.id.image_big);
        zanView = (ImageView) findViewById(R.id.zan);
        commentView = (ImageView) findViewById(R.id.comment);
        toComView = (TextView) findViewById(R.id.comment_list_to);

        //viewpage
        viewPagerFixed = (ViewPagerFixed) findViewById(R.id.viewPager);
        mAdapter = new GalleryAdapter();
        viewPagerFixed.setAdapter(mAdapter);
        viewPagerFixed.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                mPhotoInfo = photoList.get(i);
                getImageDetail();
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        mPhotoInfo = photoList.get(currentPosition);
        viewPagerFixed.setCurrentItem(currentPosition);

        jubaoText = (TextView) findViewById(R.id.jubao);
        jubaoText.setOnClickListener(this);
//        url = mPhotoInfo.getPhotoURL();
//        ImageCache.getInstance(mContext).loadBitmapToView(mImageView,url,null);
        zanView.setOnClickListener(this);
        commentView.setOnClickListener(this);
        toComView.setOnClickListener(this);
    }

    private void updateView(){

        String s = String.format(getString(R.string.zancomment),mPhotoInfo.getZanNum(),mPhotoInfo.getCommentNum());
        toComView.setText(s);

        if (!mPhotoInfo.getIsZan()) {
            zanView.setImageResource(R.drawable.zan_hong);
        }else {
            zanView.setImageResource(R.drawable.zan_white_bg);
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mPhotoInfo != null){
            getImageDetail();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(url);
//        if (cacheBitmap != null){
//            cacheBitmap.recycle();
//        }
    }

    private void getImageDetail() {
        ZhaopianDetailTask task = new ZhaopianDetailTask(mPhotoInfo.getImageId());
        task.addListener(mListener);
        detailTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.zan:
                if (!mPhotoInfo.getIsZan()){
                    return;
                }
                doZan();
                break;
            case R.id.comment:
                Intent intentc = new Intent(mContext,PublishCommentActivity.class);
                intentc.putExtra("VideoId",mPhotoInfo.getImageId());
                intentc.putExtra("type",2);
                startActivityForResult(intentc,111);
                break;
            case R.id.comment_list_to:
//                Intent intent = WebViewActivity.getIntent(mContext,"http://101.200.174.68/index.html");
//                startActivity(intent);
                Intent intent = new Intent(mContext,ZhaopianCommentActivity.class);
                intent.putExtra("photoInfo",mPhotoInfo);
                startActivity(intent);
                break;
            case R.id.jubao:
                Intent jubao_intent = new Intent(mContext,JuBaoActivity.class);
                jubao_intent.putExtra("photourl",mPhotoInfo.getPhotoURL());
                jubao_intent.putExtra("type",1);
                startActivity(jubao_intent);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 111 && resultCode ==100){
            int num = Integer.parseInt(mPhotoInfo.getCommentNum());
            num++;
            mPhotoInfo.setCommentNum(String.valueOf(num));
            String s = String.format(getString(R.string.zancomment),mPhotoInfo.getZanNum(),mPhotoInfo.getCommentNum());
            toComView.setText(s);
        }
    }

    private void doZan(){
        ZanzhaopianTask task = new ZanzhaopianTask(mPhotoInfo.getPhotoURL(),type);
        task.addListener(mListener);
        zanTaskId = getEatShowApp().getTaskManager().addTask(task);
    }



    private ShowToastTaskEventListener mListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == zanTaskId) {
                zanTaskId = -1;
                zanView.setImageResource(R.drawable.zan_hong);
                mPhotoInfo.setIsZan(false);
                int num = Integer.parseInt(mPhotoInfo.getZanNum());
                num++;
                mPhotoInfo.setZanNum(String.valueOf(num));
                String s = String.format(getString(R.string.zancomment), mPhotoInfo.getZanNum(), mPhotoInfo.getCommentNum());
                toComView.setText(s);
            }else if (taskId == detailTaskId){
                if (data != null){
                    mPhotoInfo = (PhotoInfo) data;
                    updateView();
                }
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
        }
    };


    class GalleryAdapter extends PagerAdapter {
        public GalleryAdapter() {
        }

        @Override
        public int getCount() {
            return photoList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object o) {
            return view == o;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            PhotoInfo info = photoList.get(position);
            PhotoView img = new PhotoView(mContext);
            img.setTag(info.getPhotoURL());
            ImageCache.getInstance(mContext).loadBitmapToView(img,info.getPhotoURL(),null);
            img.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            container.addView(img);
            return img;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            PhotoInfo info = photoList.get(position);
            PhotoView image = (PhotoView) container.findViewWithTag(info.getPhotoURL());
            container.removeView(image);
        }
    }
}
